layui.use(['form', 'layer', 'jquery', 'formSelects'], function () {
    var form = layui.form;
    var layer = layui.layer;
    var $ = layui.jquery;
    var formSelects = layui.formSelects;


    // 取消
    $("#closeBtn").click(function () {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    })

    // 异步加载下拉框

    $.ajax({
        type: "get",
        url: ctx + "/dept/deptSelected",
        data: {},
        success: function (data) {
            if (data != null) {
                $.each(data, function (index, item) {
                    $('#deptId').append(new Option(item.dept_name, item.id));
                    $('#deptId').val($("#dept").val())
                })
                form.render(); //更新全部表单内容
            }
        }
    });

    var empId = $("#id").val();
    formSelects.config("selectId", {
        type: "post",
        searchUrl: ctx + "/role/queryEmpRolesByEmpId?empId=" + empId,
        keyName: "roleName",
        keyVal: "id"
    }, true)


    // 提交 updateEmpRoleByEmpId
    form.on("submit(updateEmp)", function (data) {
        var index = top.layer.msg("数据提交中", {
            icon: 16,
            time: true,
            shade: 0.8
        })
        var formData = data.field;
        var url = ctx + "/emp/updateEmp";

        $.post(url, formData, function (result) {
            if (result.code == 200) {
                top.layer.close(index);
                layer.msg(result.msg)
                layer.closeAll("iframe")
                parent.location.reload();

            } else {
                top.layer.close(index);
                layer.msg(result.msg, {time: 2000});
            }
        })
        return false;

    })


})